package team3.tracking.communication;

import java.util.LinkedList;


public class PositionInfo {
	   
	   /* This queue stores the position information of clients*/
		static LinkedList<ClientData> infoBank = new LinkedList<ClientData>();	
		
	public static void setPositionInfo(ClientData pos){ 
		//this will add the ClientData to the LinkedList. If it already exists
		//in the Linked List, its values will be updated
		
		boolean added = false;
		synchronized(infoBank){

			for(int i = 0; i < infoBank.size(); i++){
				if(pos.getClientID().equals(infoBank.get(i).clientID)){
					added = true;
					infoBank.get(i).clientID=pos.getClientID();
					infoBank.get(i).latitude = pos.getLatitude();
					System.out.println("Inside stPosition Saving to InfoBank"+ infoBank.get(i).longitude);
					infoBank.get(i).longitude = pos.getLongitude();
					infoBank.get(i).timeStamp = pos.getTimeStamp();
					infoBank.get(i).ServertimeStamp = pos.getServerTimeStamp();
					infoBank.get(i).predictionVectorX = pos.getPredictionVectorX();
					infoBank.get(i).predictionVectorY = pos.getPredictionVectorY();
					System.out.println("Client Position Information updated..");
					break;
				}
			}
			if(!added){
				infoBank.add(pos);
				System.out.println("New Client Added to InformationBank..");
			}
		}
		System.out.println("Position Set! :) ...");
	}
	
	//get Position info from info Bank Linklist
	public static ClientData getPositionInfo(String clientID){
		ClientData getPose = new ClientData();

		synchronized(infoBank){
			for(int i = 0; i < infoBank.size(); i++){
				if(clientID.equals(infoBank.get(i).clientID)){
					System.out.println("Inside getPosition"+ infoBank.get(i).longitude);
					getPose.setClientID(infoBank.get(i).clientID);
					getPose.setLatitude(infoBank.get(i).latitude);
					getPose.setLongitude(infoBank.get(i).longitude);
					System.out.println("from get Pose:"+ getPose.getLongitude());
					getPose.setTimeStamp(infoBank.get(i).timeStamp);
					getPose.setServerTimeStamp(infoBank.get(i).ServertimeStamp);
					getPose.setPredictionVectorX(infoBank.get(i).predictionVectorX);
					getPose.setPredictionVectorY(infoBank.get(i).predictionVectorY);
					
					
					break;
				}
			}
		}
		
		return(getPose);
	}
	
	public static byte[] serializeData(ClientData input){
		//This serializes the ClientData object, turning it into a byte array
		
		byte[] outputByteStream;

		String outputString;
		try{
			outputString = 	input.clientID + "#" + Double.toString(input.latitude) + "#" + Double.toString(input.longitude)
			+ "#" + Long.toString(input.timeStamp) 
			+ "#" + Double.toString(input.predictionVectorX)+ "#" + Double.toString(input.predictionVectorY)+ "#";
		}
		catch(NumberFormatException e){
			outputString = "";
		}
		outputByteStream = outputString.getBytes();

		return(outputByteStream);
	}

	
	public static ClientData deserializeData(String input){
		//this deserializes the ClientData object and returns the ClientData
		
		String [] tokens;
		ClientData cData = new ClientData();
		System.out.println("Input : "+input);
		tokens = input.split("#");
		System.out.println(tokens.length);
		if(tokens.length >= 6 ){
			//when the client is sending its position fix, it sends all of the below attributes
			cData.setClientID(tokens[0]);
			cData.setLatitude(Double.parseDouble(tokens[1]));			
			cData.setLongitude(Double.parseDouble(tokens[2]));
			cData.setTimeStamp(Long.parseLong(tokens[3]));
			cData.setPredictionVectorX(Double.parseDouble(tokens[4]));
			cData.setPredictionVectorY(Double.parseDouble(tokens[5]));	
		}
		else if(tokens.length>=1)
		{   //when the client is making a query, it only sends its ID.
			cData.setClientID(tokens[0]);
			
		}
		return(cData);
	}
	

}
